/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package petstore.view.action.atendimento;

import petstore.view.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorActionForm;
import petstore.business.IComumFacade;
import petstore.model.Animal;
import petstore.model.enumeration.Sexo;
import petstore.util.Util;

/**
 *
 * @author Tom Hayden
 */
public class ProntuarioAnimalAction extends CustomDispatchAction {

    private IComumFacade comumFacade;
    
    public void setComumFacade(IComumFacade comumFacade) {
        this.comumFacade = comumFacade;
    }
    
    public ActionForward selecionarCliente(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        request.getSession().setAttribute("limparAnimal", "false");
        request.getSession().setAttribute("previousAction", "prontuarioPage.do?page=tabAnimal");

        return mapping.findForward("cliente");

    }

    public ActionForward salvar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        DynaValidatorActionForm f = (DynaValidatorActionForm) form;
        ActionErrors errors = f.validate(mapping, request);

        if (errors.isEmpty()) {
            Animal animal = this.getAnimal(f, request);
            this.saveRequest(animal, request);
            this.comumFacade.alterarAnimal(animal);
            animal = this.comumFacade.getAnimal(animal.getId());
            this.saveRequest(animal, request);
            request.setAttribute("msgSucesso", "true");
        } else {
            request.setAttribute(Globals.ERROR_KEY, errors);
            return mapping.getInputForward();
        }

        return mapping.findForward("mesmaPagina");

    }

    private Animal getAnimal(DynaValidatorActionForm f, HttpServletRequest request) {
        Animal animal = (Animal) request.getSession().getAttribute("animal");
        animal.setDescription(f.getString("description"));
        animal.setCliente(this.comumFacade.getCliente((Integer)f.get("idCliente")));
        animal.setRaca(this.comumFacade.getRaca((Integer)f.get("idRaca")));
        animal.setPelagem(this.comumFacade.getPelagem((Integer)f.get("idPelagem")));
        animal.setSexo(Sexo.valueOf(f.getString("sexo")));
        animal.setDataNascimento(Util.stringToDate("dd/MM/yyyy", f.getString("dataNascimento")));
        animal.setPedigree(new Boolean(f.getString("pedigree")));
        return animal;
    }

    private void saveRequest(Animal animal, HttpServletRequest request) {
        request.getSession().setAttribute("animal", animal);
    }

}
